
/*
   tsAPI Distribution
   color_swatch Class

   TrueSpectra Inc, 2002
   Code by: Kevin Martin
*/

function color_swatch(orientation,swatch_size,border,border_inside,cname,expandforward) {
	this.or 		= orientation;
	this.border		= border;
	this.border2		= Math.min( (swatch_size-1)/2,border_inside);
	this.cname		= (""+expandforward=="true"?true:false);
	this.expandforward 	= (""+expandforward=="false"?false:true);
	this.swatch_size	= swatch_size;

	this.targetImagesLow = new Array();

	this.DynLayer = DynLayer;
	this.DynLayer();


	this.delay = 20;
	this.writedelay = 200;
	this.targetImagesHigh = new Array();
	this.curload = 0;
	this.RGBArray = new Array();
	this.layerArray = new Array();
	this.colorArray = new Array();		//Used for Pre-Loading


	this.w = 0;
	this.h = 0;

	this.setOrientation(orientation);
};

color_swatch.prototype = new DynLayer;


color_swatch.prototype.setOrientation = function(type) { // 0=horz,1=vert
	this.horizontal = (type==color_swatch.HORIZONTAL);
	this.vertical = (type==color_swatch.VERTICAL);
};


color_swatch.prototype.addTargetImage = function( o,hires ) {
	if( hires==true ) 	{ this.targetImagesHigh[this.targetImagesHigh.length] = o; }
	else			{ this.targetImagesLow[this.targetImagesLow.length] = o; };
};

color_swatch.VERTICAL = 1;
color_swatch.HORIZONTAL = 2;

color_swatch.prototype.addColor = function( sRGB, slayer ) {
	this.RGBArray[this.RGBArray.length]	= sRGB;
	this.layerArray[this.layerArray.length]	= slayer;

/*
	//Create background Layer
        var newLayer = new DynLayer(null,0,0,this.swatch_size,this.swatch_size,'#999999' );

	//Add Color Layer
	newLayer.colorLayer = newLayer.addChild( new DynLayer(null,this.border2,this.border2,this.swatch_size-this.border2*2-1,this.swatch_size-this.border2*2-1,'#'+sRGB) );

	//Add Image Layer
	var imgLayer = new DynImage( getTSGif(DynImage.getImage(DynAPI.librarypath+'tsapi/images/spacer.tsgif&'+this.swatch_size+'x'+this.swatch_size),this.swatch_size,this.swatch_size),"javascript:"+this+".changeColor_All('"+sRGB+"','"+slayer+"');" );
        newLayer.imglayer = newLayer.addChild( imgLayer );
        newLayer.imglayer.setSize( this.swatch_size, this.swatch_size );

	this.newLayer = this.addChild(newLayer);

        if( this.or==color_swatch.VERTICAL )	{ this.newLayer.moveTo(0,this.h); }
        else					{ this.newLayer.moveTo(this.w,0); };

	var newx = this.x-(this.or==color_swatch.HORIZONTAL && this.expandforward==false ?(this.swatch_size+  (this.RGBArray.length!=1?this.border:0)  ):0);
	var newy = this.y-(this.or==color_swatch.VERTICAL   && this.expandforward==false ?(this.swatch_size+  (this.RGBArray.length!=1?this.border:0)  ):0);
	var neww = this.swatch_size+this.border + (this.or==color_swatch.HORIZONTAL?(this.w):0);
	var newh = this.swatch_size+this.border + (this.or==color_swatch.VERTICAL  ?(this.h):0);

	this.moveTo(newx,newy);
	this.setSize(neww,newh);
*/
};





color_swatch.prototype.changeColor = function( str, sRGB, sLayer ) {
  if( (i3=str.indexOf("cmp-end=1") ) >= 0 ) {
    if( (i=str.indexOf("cmp-color="+sLayer+",x")) >= 0 ) {
      i2 = str.indexOf("&",i+10);
      return str = str.substring(0,i)+"cmp-color="+sLayer+",x"+sRGB+str.substring(i2);
    };
    return str.substring(0,i3)+"cmp-color="+sLayer+",x"+sRGB+"&"+str.substring(i3);
  };
  
  return str.substring( 0,Math.min(str.indexOf("&"),str.indexOf("?"))+1) +"cmp-color="+sLayer+",x"+sRGB+"&cmp-end=1&"+str.substring( Math.min(str.indexOf("&"),str.indexOf("?"))+1 );
};



color_swatch.prototype.changeColor_All = function( sRGB, sLayer ) {
  if( this.curload < this.RGBArray.length && this.curload < this.targetImagesLow-1 ) { 
    setTimeout(this+'.changeColor_All("'+sRGB+'","'+sLayer+'")',this.delay);
    return;
  };

  for( var i=0;i<this.targetImagesLow.length; i++ ) {
    this.changeColor_nrml( this.targetImagesLow[i],sRGB,sLayer ); 
  };

  for( var i=0;i<this.targetImagesHigh.length; i++ ) {
    this.changeColor_dbuf( this.targetImagesHigh[i], sRGB, sLayer ); 
  };

  this.invokeEvent('colorchange');
};

color_swatch.prototype.changeColor_nrml = function( o, sRGB, sLayer ) {
  o.setImageSrc( this.changeColor( o.getImageSrc(),sRGB,sLayer ) );
};





color_swatch.prototype.changeColor_dbuf = function( o, sRGB, sLayer ) {
  if( o.setImageSpecial )
    o.setImageSpecial( this.changeColor( o.getImageSrc(),sRGB,sLayer ) );
  else
    o.setImageSrc( this.changeColor( o.getImageSrc(),sRGB,sLayer ) );
};

color_swatch.prototype.dbl_buff_show = function(o,isVisible) {
  if ( ! o.isLoaded()) {
    setTimeout(this+'.dbl_buff_show('+o+','+isVisible+')',this.delay);
    return;
  };
  if ( isVisible )    { setTimeout(this+'.dbl_buff_show_pause('+o+','+isVisible+')',this.writedelay); };

};

color_swatch.prototype.dbl_buff_show_pause = function(o,isVisible) {
  o.setVisible(true);
};


function getTSGif(s) {
  var i = s.indexOf("://");  var i2 = (i<0?0:s.indexOf("/",i+3));
  return s.substr(0,i2)+"/?tsimg="+s.substr(i2);
};



/**************PRELOADING IMAGES CODE********************/

color_swatch.prototype.start_loadLowRes = function() { 
	this.curload=0; 
	this.load_LowRes_Images(0); 
};

color_swatch.prototype.load_LowRes_Images = function(num) {
        if( this.curload >= this.RGBArray.length || num >= this.targetImagesLow.length ) { 
		alert( "error"+(num >= this.targetImagesLow.length ) ); 
		return;
	};
	this.colorArray[this.curload] = new Image();
	this.colorArray[this.curload].src = this.changeColor(this.targetImagesLow[num].getImageSrc(),this.RGBArray[this.curload],this.layerArray[this.curload]);
	setTimeout( this+'.check_loadLowRes('+this.curload+','+num+')',this.delay );
};

color_swatch.prototype.check_loadLowRes = function(i,num) {
	if( this.colorArray[i].complete ) {
		if( num < this.targetImagesLow.length-1 ) {
			this.load_LowRes_Images( (num+1) );
		} else {
			if( (this.curload+1) < this.RGBArray.length )	{
				this.curload++;
				this.load_LowRes_Images( 0 );
			} else {
				this.invokeEvent('preLoaddone');
			};
		};

	} else {
		setTimeout( this+'.check_loadLowRes('+i+','+num+')', this.delay );
	};

};
